<template>
    <div class="container-fluid" v-loading="loading">

        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header"><h4>消息内容设置</h4></div>
                    <div class="card-body">
                        <el-row :gutter="20">
                            <el-col :span="12">
                                <div class="form-group">
                                    <label for="summary">文本消息</label>
                                    <el-input
                                            type="textarea"
                                            placeholder="请输入内容"
                                            v-model="content"
                                            :rows="12"
                                            show-word-limit>
                                    </el-input>
                                </div>
                                <div class="form-group">
                                    <label for="imageUrl">封面图片</label>
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/api/upload"
                                            :show-file-list="false"
                                            :on-success="handleImgUrlSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </div>
                                <button type="submit" class="btn btn-primary" v-on:click="submit()">提交</button>
                            </el-col>
                            <el-col :span="6" style="margin-left:50px;border:1px solid #b3acac;">

                                <!--<label for="imageUrl">插入通配符</label>-->
                                <div class="item" v-for="item in contentSetting">
                                    <div class="form-group row m-b-6">
                                        <div class="col-xs-9">{{item.title}}</div>
                                        <div class="col-xs-3" style="text-align:right;">
                                            <a href="javascript:void(0);" v-on:click="addMessage(item)">插入</a>
                                        </div>
                                    </div>
                                </div>

                            </el-col>
                        </el-row>
                    </div>

                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "MessageEditComponent",
        data() {
            return {
                describe: '',
                id: $('meta[name="id"]').attr('content'),
                loading:true,
                contentSetting:[],
                content:'',
                imageUrl:''
            }
        },
        methods:{
            getInfo() {
                let self = this;
                let uri = "message/" + self.id;
                self.loading = true;

                axios.get(uri).then(function (data) {
                    var result = data.result;
                    self.contentSetting = result.contentSetting;
                    self.content = result.contentImgUrl.content;
                    self.imageUrl = result.contentImgUrl.imageUrl;
                    self.loading = false;
                }).catch(function (error) {
                    this.loading = false;
                });
            },
            addMessage(item){
                let self = this;
                if (self.content){
                    self.content += "\r"
                }
                self.content += item.content;
            },
            handleImgUrlSuccess: function (response) {
                let self = this;
                if (response.code === 200) {
                    self.imageUrl = response.url
                }
                console.log(response)
            },
            beforeAvatarUpload: function (item) {
                console.log(item)
            },
            submit(){

                let self = this;
                let uri = "message/"+self.id;
                self.loading = true;
                let postData = {
                    body: self.content, imageUrl:self.imageUrl};

                axios.put(uri,postData).then(function (result) {
                    if (result) {
                        window.history.back();
                    } else {
                        self.$message.error("创建文章！");
                    }
                }).catch(function (error) {
                    self.loading = false;
                });
            }
        },
        mounted(){
            this.getInfo();
        }
    }
</script>

<style scoped>
    .el-row {
        margin-bottom: 20px;
    &:last-child {
         margin-bottom: 0;
     }
    }


    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .el-upload__input{
        display: none;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>
